﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Sealight.Core.DataAdapter;
using Sealight.CoreAPI;
using Sealight.API.Ioc;
using Sealight.UI.WebUI.Models;

namespace Sealight.UI.WebUI.Services
{
    public class ProductService
    {
        #region Product Attribute
        public static void AddUpdateProductAttribute(ProductAttributeViewModel data)
        {
            product_attribute item = new product_attribute
            {
                id = data.ID,
                name = data.Name,
                description = data.Description
            };
            IProductService svc = IocContainer.Container().Resolve<IProductService>();
            svc.AddProductAttribute(item);           
        }
        public static IList<ProductAttributeViewModel> GetAllProductAttribute()
        {
            IProductService svc = IocContainer.Container().Resolve<IProductService>();
            var result = svc.GetAllProductAttribute();
            IList<ProductAttributeViewModel> data = null;
            if (result != null)
            {
                data = (from c in result
                        select new ProductAttributeViewModel
                            {
                                ID = c.id,
                                Name = c.name,
                                Description = c.description
                            }).ToList();
            }

            return data;
        }
        public static ProductAttributeViewModel GetProductAttribute(Guid id)
        {
            IProductService svc = IocContainer.Container().Resolve<IProductService>();
            var result = svc.GetProductAttribute(id);
            ProductAttributeViewModel data = null;
            if (result != null)
            {
                data = new ProductAttributeViewModel
                {
                    ID = result.id,
                    Name = result.name,
                    Description =result.description
                };
            }
            return data;
        }
        public static void DeleteProductAttribute(Guid id)
        {
            IProductService svc = IocContainer.Container().Resolve<IProductService>();
            svc.DeleteProductAttribute(id);           
        }       
        #endregion
        #region Category
        public static IList<ProductCategoryViewModel> GetAllCategory()
        {
            IProductService svc = IocContainer.Container().Resolve<IProductService>();
            var result = svc.GetAllCategory();
            IList<ProductCategoryViewModel> data = new List<ProductCategoryViewModel>();
            if (result != null)
            {
                foreach (var c in result)
                {
                   var item= new ProductCategoryViewModel
                   {
                       CategoryID = c.id,
                       ParentID = c.parent_id,
                       Name = c.name
                   };
                   if (c.childrens != null)
                   {
                       item.Childrens = (from i in c.childrens
                                         select new ProductCategoryViewModel
                                             {
                                                 CategoryID = i.id,
                                                 Name = i.name,
                                                 ParentID = i.parent_id
                                             }).ToList();
                   }

                   data.Add(item);
                }
            }
            return data;
        }
        public static IList<product_category> GetAllProductCategory()
        {
            IProductService svc = IocContainer.Container().Resolve<IProductService>();
            var result = svc.GetAllCategory();

            return result;
        }
        public static IList<product_category> GetAllCategoryByParentId(Guid parentId)
        {
            IProductService svc = IocContainer.Container().Resolve<IProductService>();
            var result = svc.GetAllCategoryByParentId(parentId);
            return result;
        }
        public static product_category AddOrUpdateCategory(product_category data)
        {
            IProductService svc = IocContainer.Container().Resolve<IProductService>();
            return svc.AddOrUpdateCategory(data);          
        }
        public static product_category GetCategoryById(Guid id)
        {
            IProductService svc = IocContainer.Container().Resolve<IProductService>();
            return svc.GetCategoryById(id);
        }
        public static void DeleteCategory(Guid id)
        {
            IProductService svc = IocContainer.Container().Resolve<IProductService>();
            svc.DeleteCategory(id);
        }
        #endregion
        #region specification attribute
        public static void AddUpdateSpecificationAttribute(SpecificationAttributeViewModel data)
        {
            specification_attribute item = new specification_attribute
            {
                CategoryId = data.Category,
                display_order = data.DisplayOrder,
                id = data.ID,
                name = data.Name
            };
            IProductService svc = IocContainer.Container().Resolve<IProductService>();
            svc.AddSpecificationAttribute(item);           
        }
        public static void DeleteSpecificationAttribute(Guid id)
        {           
            IProductService svc = IocContainer.Container().Resolve<IProductService>();
            svc.DeleteSpecificationAttribute(id);
        }
        public static IList<SpecificationAttributeViewModel> GetAllSpecificationProductAttribute()
        {
            IProductService svc = IocContainer.Container().Resolve<IProductService>();
            var result = svc.GetAllSpecificationProductAttribute();
            IList<SpecificationAttributeViewModel> data = null;
            if (result != null)
            {
                data = (from c in result select new SpecificationAttributeViewModel { ID = c.id, Name=c.name, Category = c.CategoryId, DisplayOrder = c.display_order }).ToList();
            }
            return data;
        }
        #endregion
        #region specification attribute option
        public static void AddUpdateSpecificationAttributeOption(SpecificationAttributeOptionViewModel data)
        {
            specification_attribute_option item = new specification_attribute_option
            {
                display_order = data.DisplayOrder,
                id = data.ID,
                name = data.Name,
                specification_attribute = new specification_attribute
                {
                    id = data.SpecificationAttribute.ID
                }
            };
            IProductService svc = IocContainer.Container().Resolve<IProductService>();
            svc.AddSpecificationAttributeOption(item);
        }
        public static void DeleteSpecificationAttributeOption(Guid id)
        {
            IProductService svc = IocContainer.Container().Resolve<IProductService>();
            svc.DeleteSpecificationAttributeOption(id);
        }
        public static IList<SpecificationAttributeOptionViewModel> GetAllOptionBySpecificationId(Guid id)
        {
            IProductService svc = IocContainer.Container().Resolve<IProductService>();
            var result = svc.GetAllOptionBySpecificationId(id);
            IList<SpecificationAttributeOptionViewModel> data = null;
            if (result != null)
            {
                data = (from c in result
                        select new SpecificationAttributeOptionViewModel
                            {
                                ID = c.id,
                                Name = c.name,
                                DisplayOrder = c.display_order,
                                SpecificationAttribute = new SpecificationAttributeViewModel
                                {
                                    ID = c.specification_attribute.id,
                                    Name = c.specification_attribute.name,
                                    DisplayOrder = c.specification_attribute.display_order,
                                    Category = c.specification_attribute.CategoryId
                                }
                            }).ToList();
            }
            return data;
        }
        #endregion
        #region Product
        public static product GetProduct(Guid id)
        {
            IProductService svc = IocContainer.Container().Resolve<IProductService>();
            return svc.GetProductByID(id);
        }
        public static product AddOrUpdateProduct(product data)
        {
            IProductService svc = IocContainer.Container().Resolve<IProductService>();
            return svc.AddOrUpdate(data);
        }
        public static IList<product_category_map> SaveCategoryToProduct(IList<product_category_map> category, Guid productId)
        {
            IProductService svc = IocContainer.Container().Resolve<IProductService>();
            return svc.MapCategoryToProduct(category, productId);
        }
        public static picture SavePictureForProduct(Guid productId, string image_name)
        {
            IProductService svc = IocContainer.Container().Resolve<IProductService>();
            return  svc.AddPictureToProduct(new picture
            {
                picture_url = image_name
            }, productId);
        }
        public static IList<related_product> GetRelatedProducts(Guid productId)
        {
            IProductService svc = IocContainer.Container().Resolve<IProductService>();
            return svc.GetRelatedProducts(productId);
        }
        public static void SaveRelatedProduct(related_product data)
        {
            IProductService svc = IocContainer.Container().Resolve<IProductService>();
            svc.AddRelatedProduct(data);
        }

        public static void RemoveRelatedProduct(Guid relateProductId)
        {
            IProductService svc = IocContainer.Container().Resolve<IProductService>();
            svc.RemoveRelatedProduct(relateProductId);
        }


        public static IList<product> SearchProduct(string keyword, Guid categoryId)
        {
            IProductService svc = IocContainer.Container().Resolve<IProductService>();
            var data = svc.SearchProduct(keyword, categoryId);

            if (data == null) data = new List<product>();

            return data;
        }
        public static void DeleteCategoryMapToProduct(Guid id)
        {
            IProductService svc = IocContainer.Container().Resolve<IProductService>();
            svc.DeleteCategoryMapToProduct(id);
        }
        public static void DeletePictureMapToProduct(Guid id)
        {
            IProductService svc = IocContainer.Container().Resolve<IProductService>();
            svc.DeletePictureMapToProduct(id);
        }
        #endregion

        #region Product Variant
        public static product_variant GetProductVariant(Guid id)
        {
            IProductService svc = IocContainer.Container().Resolve<IProductService>();
            return svc.GetProductVariantByID(id);
        }
        public static product_variant AddOrUpdateProductVariant(product_variant data)
        {
            IProductService svc = IocContainer.Container().Resolve<IProductService>();
            return svc.AddOrUpdateProductVariant(data);
        }
        
        #endregion

        #region Product Variant Attribute

        public static IList<ProductVariantAttributeViewModel> GetProductVariantAttribute(Guid id)
        {
            IProductService svc = IocContainer.Container().Resolve<IProductService>();
            var result = svc.GetProductVariantAttribute(id);
            var data = new List<ProductVariantAttributeViewModel>();
            if (result != null)
            {
                data = (from c in result
                        select new ProductVariantAttributeViewModel
                        {
                            AttributeControlType = c.attribute_control_type.ToString(),
                            AttributeControlTypeID = (int)c.attribute_control_type,
                            DisplayOrder = c.display_order,
                            IsRequired = c.is_required,
                            ProductAttribute = c.product_attribute.name,
                            ProductAttributeID = c.product_attribute.id,
                            TextPrompt = c.text_prompt,
                            ProductVariantAttributeID = c.id
                        }).ToList();
            }
            return data;
        }

        public static void AddProductVariantAttribute(ProductVariantAttributeViewModel data)
        {
            if (data != null && !string.IsNullOrEmpty( data.ProductAttribute))
            {
                IProductService svc = IocContainer.Container().Resolve<IProductService>();

                product_variant_attribute item = new product_variant_attribute
                {
                    attribute_control_type = (Sealight.Core.Common.AttributeControlType)Enum.Parse(typeof(Sealight.Core.Common.AttributeControlType), data.AttributeControlType),
                    display_order = data.DisplayOrder,
                    id = data.ProductVariantAttributeID,
                    text_prompt = data.TextPrompt,
                    is_required = data.IsRequired,
                    product_attribute = new product_attribute { id = new Guid(data.ProductAttribute) },
                    product_variant = new product_variant { id = data.ProductVariantID }
                };
                svc.AddProductVariantAttribute(item);
            }
        }
        public static void DeleteProductVariantAttribute(Guid id)
        {
            IProductService svc = IocContainer.Container().Resolve<IProductService>();
            svc.DeleteProductVariantAttribute(id);
        }
        #endregion

        #region Product Variant Attribute Value

        public static IList<ProductVariantAttributeValueViewModel> GetProductVariantAttributeValue(Guid id)
        {
            IProductService svc = IocContainer.Container().Resolve<IProductService>();
            var result = svc.GetProductVariantAttributeValue(id);
            var data = new List<ProductVariantAttributeValueViewModel>();
            if (result != null)
            {
                data = (from c in result
                        select new ProductVariantAttributeValueViewModel
                        {
                          DisplayOrder = c.display_order,
                          IsPreSelected = c.is_pre_selected,
                          Name = c.name,
                          PriceAdjustment = c.price_adjustment,
                          WeightAdjustment = c.weight_adjustment,
                          ProductVariantAttributeValueID = c.id,
                          ProductVariantAttributeID = c.product_variant_attribute_id
                        }).ToList();
            }
            return data;
        }

        public static void AddProductVariantAttributeValue(ProductVariantAttributeValueViewModel data)
        {
            if (data != null && data.ProductVariantAttributeID != Guid.Empty)
            {
                IProductService svc = IocContainer.Container().Resolve<IProductService>();

                product_variant_attribute_value item = new product_variant_attribute_value
                {
                    display_order = data.DisplayOrder,
                    id = data.ProductVariantAttributeValueID,
                    is_pre_selected = data.IsPreSelected,
                    name = data.Name,
                    price_adjustment = data.PriceAdjustment,
                    weight_adjustment = data.WeightAdjustment,
                    product_variant_attribute_id = data.ProductVariantAttributeID
                };
                svc.AddProductVariantAttributeValue(item);
            }
        }
        public static void DeleteProductVariantAttributeValue(Guid id)
        {
            IProductService svc = IocContainer.Container().Resolve<IProductService>();
            svc.DeleteProductVariantAttributeValue(id);
        }
        #endregion

        #region Specification Attribute Option For Product

        public static IList<ProductSpecificationAttributeViewModel> GetSpecificationOptionAttributeForProduct(Guid id)
        {
            IProductService svc = IocContainer.Container().Resolve<IProductService>();
            var result = svc.GetSpecificationOptionAttributeForProduct(id);
            var data = new List<ProductSpecificationAttributeViewModel>();
            if (result != null)
            {
                data = (from c in result
                        select new ProductSpecificationAttributeViewModel
                        {
                            DisplayOrder = c.display_order,
                            AllowFiltering = c.allow_filtering,
                            OptionName = c.option.name,
                            ShowOnProductPage = c.show_on_product_page,
                            SpecificationAttributeOptionID = c.option.id,
                            ID = c.id,
                            ProductID = c.product.id                          
                        }).ToList();
            }
            return data;
        }

        public static void MapProductSpecificationAttribute(ProductSpecificationAttributeViewModel data)
        {
            if (data != null)
            {
                IProductService svc = IocContainer.Container().Resolve<IProductService>();

                product_specification_attribute item = new product_specification_attribute
                {
                    id = data.ID,
                    product = new product { id = data.ProductID },
                    option = new specification_attribute_option {id = data.SpecificationAttributeOptionID },
                    allow_filtering = data.AllowFiltering,
                    display_order = data.DisplayOrder,
                    show_on_product_page = data.ShowOnProductPage
                };
                svc.MapProductSpecificationAttribute(item);
            }
        }
        public static void DeleteProductSpecificationAttribute(Guid id)
        {
            IProductService svc = IocContainer.Container().Resolve<IProductService>();
            svc.DeleteProductSpecificationAttribute(id);
        }
        public static ProductSpecificationAttributeViewModel GetProductSpecificationAttribute(Guid id)
        {
            IProductService svc = IocContainer.Container().Resolve<IProductService>();
            var c = svc.GetProductSpecificationAttribute(id);
            ProductSpecificationAttributeViewModel data = null;
            if (c != null)
            {
                data =  new ProductSpecificationAttributeViewModel
                        {
                            DisplayOrder = c.display_order,
                            AllowFiltering = c.allow_filtering,
                            OptionName = c.option.name,
                            ShowOnProductPage = c.show_on_product_page,
                            SpecificationAttributeID = c.option.specification_attribute.id,
                            SpecificationAttributeOptionID = c.option.id,
                            ID = c.id,
                            ProductID = c.product.id
                        };
            }
            return data;
        }
        #endregion
    }
}